var app = getApp()
var utils = require('../../../utils/utils.js')
Page({
  data: {
    showTipWords:false,
    tipWords: '暂无订单'
  },
  onLoad: function(options) {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    that.freshOrderList();
    wx.hideLoading();
  },
  /**
   * 刷新当前页面
   * @param event
   */
  freshOrderList:function(event){
    var that = this
    //获取订单-start
    var param = {"openId":app.globalData.openid}
    wx.request({
      url: utils.sysConst.userOrderPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        if (res.data.code == 1 && res.data.data != null){
          that.setData({
            userOrderPath :res.data.data
          })
        }else{
          that.setData({
            showTipWords:true,
          })
        }
      }
    })
  },

  //订单详情-start
  detail:function(event){
    wx.navigateTo({
      url: '/pages/personal/orderDetail/orderDetail?id='+event.currentTarget.dataset.id,
    })
  },
  //取消订单-start
  cancel:function(event){
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '确认取消订单',
      success (res) {
        if (res.confirm) {
          // start
          wx.showLoading({
            title: '处理中...',
          })
          var param = {"orderId":event.currentTarget.dataset.id,"reason":"用户操作取消"}
          wx.request({
            url: utils.sysConst.cancelOrderPath,
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
            },
            data: param,
            dataType: "json",
            success: function(res) {
              wx.hideLoading();
              that.freshOrderList();
              if (res.data.code == 1){
                wx.showToast({
                  title: '取消成功',
                  icon: 'success',
                  duration: 2000
                })
              }else{
                wx.showToast({
                  title:"取消失败："+res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
          // end
        } else if (res.cancel) {
         // console.log('用户点击取消')
        }
      }
    })
  },
  //确认收货-start
  takeDelivery:function(event){
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '请确认已经收到货物',
      success (res) {
        if (res.confirm) {
          //  start
          wx.showLoading({
            title: '处理中...',
          })
          var param = {"orderId":event.currentTarget.dataset.id}
          wx.request({
            url: utils.sysConst.takeDeliveryPath,
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
            },
            data: param,
            dataType: "json",
            success: function(res) {
              wx.hideLoading();
              that.freshOrderList();
              if (res.data.code == 1){
                wx.showToast({
                  title: '操作成功',
                  icon: 'success',
                  duration: 2000
                })
              }else{
                wx.showToast({
                  title:"操作失败："+res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })

          //   end
        } else if (res.cancel) {
          //console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


})

